package com.itheima.service;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import com.itheima.dao.ProductDao;
import com.itheima.domain.Category;
import com.itheima.domain.Order;
import com.itheima.domain.OrderItem;
import com.itheima.domain.Pagebean;
import com.itheima.domain.Product;
import com.itheima.utils.DataSourceUtils;

public class ProductService {
	
	//获得热门商品
	public List<Product> findHotProductList() {
		ProductDao dao = new ProductDao();
		List<Product> hotProductList=null;
		try {
			hotProductList = dao.findHotProductList();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return hotProductList;
	}

	public List<Product> findNewProductList() {
		ProductDao dao = new ProductDao();
		List<Product> newProductList=null;
		try {
			newProductList = dao.findNewProductList();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return newProductList;
	}

	public List<Category> findAllCategory() {
		ProductDao dao = new ProductDao();
		List<Category> categoryList=null;
		try {
			categoryList = dao.findAllCategory();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return categoryList;
	}

	public Pagebean findProductListByCid(String cid,int currentPage,int currentCount) {
		ProductDao dao =new ProductDao();
		//封装一个pagebean 返回web层
		Pagebean<Product> pagebean = new Pagebean<Product>();
		//封装当前页
		
		pagebean.setCurrentPage(currentPage);
		pagebean.setCurrentCount(currentCount);
		int totalCount =0;
		try {
			totalCount =dao.getCount(cid);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		pagebean.setTotalCount(totalCount);
		int totalPage= (int) Math.ceil(1.0*totalCount/currentCount);
		pagebean.setTotalPage(totalPage);
		
		int index=(currentPage-1)*currentCount;
		List<Product> list =null;
		try {
			list =dao.findProductByPage(cid,index,currentCount);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		pagebean.setList(list);
		return pagebean;
	}

	public Product findProductByPid(String pid) {
		ProductDao dao =new ProductDao();
		Product product =null;
		try {
			product = dao.findProductByPid(pid);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return product;
	}
	//提交订单 将订单的数据和订单项的数据存储到数据库中
	public void submitOrder(Order order) {
		ProductDao dao =new ProductDao();
		try {
			//开启事物
			DataSourceUtils.startTransaction();
			//调用dao存储order表数据的方法
			dao.addOrders(order);
			//调用dao存储orderitem表数据的方法
			dao.addOrderItem(order);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			try {
				DataSourceUtils.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			e.printStackTrace();
		} finally {
			try {
				DataSourceUtils.commitAndRelease();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		
	}

	public void updateOrderAddr(Order order) {
		ProductDao dao =new ProductDao();
		try {
			dao.updateOrderAddr(order);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	public void updateOrderState(String r6_Order) {
		ProductDao dao =new ProductDao();
		try {
			dao.updateOrderState(r6_Order);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	//获得指定用户的订单集合
	public List<Order> findAllOrders(String uid) {
		ProductDao dao =new ProductDao();
		List<Order> orderList=null;
		try {
			orderList = dao.findAllOrders(uid);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return orderList;
	}

	public List<Map<String, Object>> findAllOrderItemByOid(String oid) {
		ProductDao dao =new ProductDao();
		List<Map<String, Object>> orderItemList=null;
		try {
			orderItemList = dao.findAllOrderItemByOid(oid);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return orderItemList;
	}

}
